package com.hxkcloud.nurse.popup;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.utils.BrightnessUtils;
import com.hxkcloud.nurse.wheelview.WheelView;
import com.hxkcloud.nurse.wheelview.adapter.NumericWheelAdapter;

/**
 * Created by william_sun on 2016-10-08.
 * Description:时分选择pop
 */
public class HourMinuteSelectPop extends PopupWindow implements View.OnClickListener {
    private Activity context;
    private WheelView wheel_hour;
    private WheelView wheel_min;

    private Button bt_cancel;
    private Button bt_sure;

    private onHourMinuteSelectListener listener;

    public HourMinuteSelectPop(Activity context, onHourMinuteSelectListener listener) {
        this.context = context;
        this.listener = listener;
        View rootView = LayoutInflater.from(context).inflate(R.layout.popup_layout_dateym, null);
        setContentView(rootView);

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable(0x00000000));

//        setAnimationStyle(R.style.popupwindow_anim_default);
        initViews(rootView);
        initDatas();
        initListener();
    }

    private void initViews(View rootView) {
        wheel_hour = (WheelView) rootView.findViewById(R.id.wheel_selectym_year);
        wheel_min = (WheelView) rootView.findViewById(R.id.wheel_selectym_month);
        bt_sure = (Button) rootView.findViewById(R.id.bt_selectym_sure);
        bt_cancel = (Button) rootView.findViewById(R.id.bt_selectym_cancel);
    }

    private void initDatas() {

        NumericWheelAdapter numericWheelAdapter3 = new NumericWheelAdapter(context, 0, 23, "%02d");
        wheel_hour.setViewAdapter(numericWheelAdapter3);
        wheel_hour.setCyclic(false);

        NumericWheelAdapter numericWheelAdapter4 = new NumericWheelAdapter(context, 0, 59, "%02d");
        wheel_min.setViewAdapter(numericWheelAdapter4);
        wheel_min.setCyclic(false);

        wheel_hour.setVisibleItems(5);
        wheel_min.setVisibleItems(5);

        wheel_hour.setCurrentItem(0);
        wheel_min.setCurrentItem(0);
    }

    public void setCurrentTime(int hour,int min){
        wheel_hour.setCurrentItem(hour);
        wheel_min.setCurrentItem(min);
    }

    private void initListener() {
        bt_sure.setOnClickListener(this);
        bt_cancel.setOnClickListener(this);

        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                BrightnessUtils.lightOn(context);
            }
        });
    }

    @Override
    public void onClick(View v) {
        dismiss();
        switch (v.getId()) {
            case R.id.bt_selectym_sure:
                String hour = wheel_hour.getCurrentItem() < 10 ? "0" + wheel_hour.getCurrentItem() : wheel_hour.getCurrentItem() + "";
                String min = wheel_min.getCurrentItem() < 10 ? "0" + wheel_min.getCurrentItem() : wheel_min.getCurrentItem() + "";
                listener.onSelect(hour + ":" + min);
                break;
            case R.id.bt_selectym_cancel:

                break;
        }
    }

    public interface onHourMinuteSelectListener {
        void onSelect(String time);
    }
}
